fn main() {
    let mut x = 5;
    println!("The value of x is: {}", x);
    x = 6;
    println!("The value of x is: {}", x);

    println!("==========");

    // 常量不可以用mut修饰
    const MAX_POINTS: u32 = 100_0000;

    println!("MAX_POINTS = {}", MAX_POINTS);
    let y = 1;

    let y = y + 1;

    let y = y * 2;

    println!("y: {}", y);

    let m = 20;

    let m = 10 * m;

    println!("m = {}", m);

    println!("==========",);
    let spaces = "      ";

    // 通过let定义是个新的变量, 这这里数据类型当然也变了
    let spaces = spaces.len();

    println!("spaces: {}", spaces);

    println!("==============");

    let x1 = 2.0; // f64 双精度

    let x2: f32 = 3.0; // f32 单精度

    println!("x = {}, y = {}", x, y);

    let t = true;

    let f: bool = false;
    println!("t: {}, f: {}", t, f);

    let c = 'z';
    let z = 'ℤ';
    let h = 'N';

    println!("c: {}, z: {}, h: {}", c, z, h);

    // 复合类型
    // 元组 tuple 数组 array
    // 类似es6 解构
    let (name, version) = ("rust", 1.38);
    println!("name: {}, version: {}", name, version);

    // 不需要的直接丢弃
    let (_, _, z) = (1, 2, 3);

    println!("z = {}", z);

    // 注意string类型
    let tu: (i32, &str) = (22, "scala");

    // tupe.x 获取元组元素
    println!("lang: {}", tu.1);

    println!("================");

    let arr = [2, 3, 6, 7, 8, 9, 10];

    // 打印数组,数组是复合类型. 需要{:?}
    println!("arr: {:?}", arr);
    // 下标从0开始
    println!("第一个元素: {}, arr len: {}", arr[0], arr.len());

    println!("function",);

    print_hello();

    let ret = add(2, 3);
    println!("ret: {}", ret);
    println!("ret: {}", add(4, 5));

    println!("inc: {}", inc(10));

    let y = {
        let x = 3;
        x + 1 // 表达式结尾没有分号, 如果加上分号 就变成语句了
    };
    println!("The value of y is: {}", y);

    println!("five: {}", five());

    println!("plus_one: {}", plus_one(99));

    let num = 10;

    if num != 0 {
        println!("true");
    } else {
        println!("fasle");
    }

    let condition = true;
    let number = if condition { 5 } else { 6 };
    println!("The value of number is: {}", number);

    loop_sample();
    while_sample();
    loop_arr();
    for_arr();

    for number in (1..11).rev() {
        println!("number: {}", number);
    }
}

/**
 * for 循环
 */
fn for_arr() {
    let arr = [10, 20, 30, 40, 50, 60, 70, 80, 90];

    for item in arr.iter() {
        println!("item: {}", item);
    }
}
/**
 * while 循环
 */
fn loop_arr() {
    let arr = [10, 20, 30, 40, 50, 60, 70, 80, 90];
    let mut i = 0;
    while i < arr.len() {
        // println!("{}", i);
        println!("element[{}]: {} ", i, arr[i]);
        i += 1;
    }
}

fn while_sample() {
    let mut counter = 0;
    while counter != 100 {
        counter += 1;
        println!("counter: {}", counter);
    }
    println!("end",)
}

fn loop_sample() {
    // loop {
    //     println!("hello",)
    // }

    let mut counter = 0;

    let result = loop {
        counter += 1;
        if counter == 100 {
            break counter * 2;
        }
    };

    println!("result: {}", result);
}

fn print_hello() {
    println!("hello");
}

/**
 *两个整数之和
 */
fn add(a: i32, b: i32) -> (i32) {
    return a + b;
}
fn inc(n: i32) -> i32 {
    n + 1
}

fn five() -> i8 {
    // return 5;
    5 // 返回结果不加引号或者使用return 语句
}

fn plus_one(x: i32) -> i32 {
    x + 1
}
